/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyDate;

import javax.microedition.lcdui.*;
import java.util.*;
import sUtils.*;
import javax.microedition.pim.*;


/**
 *
 * @author thomas.s
 */
public class MyDateEntry {

    private String desc=null, full=null;
    public String uid=null;
    public long   time=0;
    public long   end=0;
    public int    type=0; 
    // 0=Termin, 1=Jahrestag, 2=Feiertag, 3=Meno, 4=Jahrestag(zero), 5=ToDo
    public int    day=0, month=0, year=0;
    public Event  ev=null;



    public MyDateEntry( String s, long t, long e, int ty, String id, Event pev )
    {
        desc = s;
        time = t;
        end = e;
        uid = id;
        type = ty;
        ev = pev;
        if (type==4)
        {
            Calendar c = Calendar.getInstance();
            Date d = new Date();
            d.setTime(t);
            c.setTime(d);
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH)+1;
            day = c.get(Calendar.DAY_OF_MONTH);

        } else
        {
            Calendar c = Calendar.getInstance();
            Date d = new Date();
            d.setTime(t);
            c.setTime(d);
            int hh = c.get( Calendar.HOUR_OF_DAY );
            int mm = c.get( Calendar.MINUTE );
            StringBuffer fdesc = new StringBuffer(20);
            if (hh<10) fdesc.append('0'); fdesc.append(hh); fdesc.append(':');
            if (mm<10) fdesc.append('0'); fdesc.append(mm); fdesc.append(" ");
            fdesc.append(s);
            full = fdesc.toString();
        }
    }

    public String getDesc( long day )
    {
        return (full!=null && day<time && day+3600000*24>time) ? full : desc;
    }

    public String getText() { return desc; }

    public int width( Font f, long start )
    {
        String s = getDesc(start);
        return s==null ? 0 : f.stringWidth(s);
    }

    public int getMinutes()
    {
        Calendar c = Calendar.getInstance();
        Date d = new Date();
        d.setTime(time);
        c.setTime(d);
        return c.get( Calendar.HOUR_OF_DAY )*60 + c.get( Calendar.MINUTE );
    }

    public int getLen()
    {
        return (int)((end-time)/60000);
    }

    public void addTo( MyDateDay d )
    {
        if (type==4)
        {
            if ((d.month==month) && (d.day==day) && (d.year>=year))
            {
                String s = desc + " " + (d.year - year) + Lang.word(". Birthday",". Geburtstag");
                d.addEntry( new MyDateEntry( s, d.timeStart, d.timeEnd, 1, "", null));
            }
        }
        else
        {
            if ((d.timeStart>=time && d.timeEnd<=end)
               || (time>=d.timeStart && end<=d.timeEnd)
               || (d.timeStart>=time && d.timeStart<end)
               || (d.timeEnd>time && d.timeEnd<=end))
                d.addEntry(this);
        }
    }

}
